package com.cez.itstudio01.controller;

import com.cez.itstudio01.mapper.UserMapper;
import com.cez.itstudio01.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * author: chenez
 * date: 2022/9/7 22:53
 * description:
 */
@Controller
public class loginController {
    @Autowired
    UserMapper userMapper;
    /**
     * @Description 登录判断
     * @Date 2022/9/10 14:52
     * @param ID
     * @param password
     *@return java.lang.Object
     **/
    @RequestMapping("/index/index/passwordValidate")
    @ResponseBody
    public Object login(String ID, String password, HttpSession session){
        User user = userMapper.login(ID, password);
        HashMap res = new HashMap();
        if(user!=null&&"1".equals(user.getLimit())){
            res.put("code","1");
            res.put("message","登陆成功");
            res.put("url","/index/admin/index?ID="+ID);
            return res;
        }
        if(user!=null&&"2".equals(user.getLimit())){
            session.setAttribute("stu.ID",user.getId()+"");
            res.put("code","1");
            res.put("message","登陆成功");
            res.put("url","/index/stu/index?ID="+ID);
            return res;
        }
        else {
            res.put("code","0");
            res.put("message","用户名或密码错误");
            return res;
        }
    }
    @GetMapping("/index/stu/index")
    public Object userLogin(){
        return "redirect:/stu/index.html";
    }
    @GetMapping("/index/admin/index")
    public Object adminLogin(){
        return "redirect:/admin/index.html";
    }
    @GetMapping("/")
    public String login(){return "/book/index.html";}

}
